package com.student.health.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student.health.advice.ExceptionEnums;
import com.student.health.advice.MyException;
import com.student.health.dao.CheckInfoMapper;
import com.student.health.dao.UserMapper;
import com.student.health.entity.CheckInfo;
import com.student.health.entity.User;
import com.student.health.service.UserService;
import com.student.health.utils.DTO.Condition;
import com.student.health.utils.DTO.ProjectUrlConfig;
import com.student.health.utils.DTO.TemplateInfo;
import com.student.health.utils.DTO.TranTemplate;
import com.student.health.utils.HttpClientUtil;
import com.student.health.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-12
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private CheckInfoMapper checkInfoMapper;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @Override
    public User login(User user) {
        String userName = user.getUsername();
        String password = user.getPassword();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            User loginUser = (User) subject.getPrincipal();
            if (loginUser != null) {
                return loginUser;
            }
        } catch (AuthenticationException e) {
            log.info("登陆失败:" + e.getMessage());
            throw e;
        }
        return null;
    }

    @Override
    public User selectByKey(Integer userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public List<User> selectUserByRoleId(Integer roleId) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getRoleId, roleId);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        return users;
    }

    @Override
    public boolean saveByuserName(User user) {
        if (user.getRoleId() == null) {
            user.setRoleId(2);
        }
        if (user.getUsername() == null) {
            user.setUsername(user.getStuNo());
        }
        if (user.getPassword() == null) {
            user.setPassword("123");
        }
        if (StringUtils.isBlank(user.getUsername())) {
            throw new MyException(ExceptionEnums.ADD_ERROR);
        }
        QueryWrapper<User> username = new QueryWrapper<User>().eq("username", user.getUsername());
        User one = userMapper.selectOne(username);
        if (one != null) {
            throw new MyException(ExceptionEnums.ACCOUNT_IS_EXIT);
        }
        user.setCreateDatetime(new Date());
        user.setUpdateDatetime(new Date());
        user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Page<User> selectPage(Condition condition) {
        Page<User> userPage = new Page<>(condition.getPageNum(), condition.getPageSize());
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String college = (String) condition.getClauses().get(0).getValue();
        String major = (String) condition.getClauses().get(1).getValue();
        String name = (String) condition.getClauses().get(2).getValue();
        String stuNo = (String) condition.getClauses().get(3).getValue();
        if ("".equals(college) && "".equals(major) && "".equals(name) && "".equals(stuNo)) {
            userLambdaQueryWrapper.orderByDesc(User::getUpdateDatetime);
        } else {
            userLambdaQueryWrapper.like(User::getCollege, college)
                    .like(User::getMajor, major)
                    .like(User::getName, name)
                    .like(User::getStuNo, stuNo)
                    .orderByDesc(User::getUpdateDatetime);
        }

        Page<User> page = userMapper.selectPage(userPage, userLambdaQueryWrapper);
        log.info("条件分页后的用户数据{}", page.getRecords());
        return page;
    }

    @Override
    public User updateById(Integer id) {
        User user = userMapper.selectById(id);
        if (user != null) {
            return user;
        }
        throw new MyException(ExceptionEnums.UPDATE_ERROR);
    }

    @Override
    public ResponseEntity<User> update(User user) {
        userMapper.updateById(user);
        return ResponseEntity.ok(user);
    }

    public static String access_token;

    @Override
    public void userNotify(String firstStr) {
        //获取token
        String tokenResult = HttpClientUtil.doGet(projectUrlConfig.getGetTokenUrl() + "&appid=" + projectUrlConfig.getMpAppId() + "&secret=" + projectUrlConfig.getMpAppSecret());
        JSONObject tokenJsonObject = JSON.parseObject(tokenResult);
        String accessToken = tokenJsonObject.getString("access_token");
        if (accessToken != null && accessToken.length() > 0) {
            List<User> users = this.userMapper.selectList(new QueryWrapper<>());
            for (User user : users) {
                if(user.getOpenId()!=null &&user.getOpenId().length()>0 ){
                    CheckInfo currentCheckInfo = checkInfoMapper.getCurrentCheckInfo(user.getId());
                    if(currentCheckInfo == null){
                        return;
                    }
                    TranTemplate tranTemplate = new TranTemplate();
                    tranTemplate.setTouser(user.getOpenId());
                    tranTemplate.setTemplate_id(projectUrlConfig.getTemplateId());
                    Map<String, TemplateInfo> map = new HashMap<>();

                    TemplateInfo templateInfo = new TemplateInfo();
                    templateInfo.setValue(firstStr);
                    map.put("first", templateInfo);

                    TemplateInfo templateInfo2 = new TemplateInfo();
                    if(firstStr.contains("早")){
                        templateInfo2.setValue(currentCheckInfo.getBreakfast());
                    }
                    if(firstStr.contains("中")){
                        templateInfo2.setValue(currentCheckInfo.getLunch());
                    }
                    if(firstStr.contains("晚")){
                        templateInfo2.setValue(currentCheckInfo.getDinner());
                    }
                    map.put("remark", templateInfo2);

                    tranTemplate.setData(map);
                    String data = JSON.toJSONString(tranTemplate);
                    String res = HttpClientUtil.doPostJson(projectUrlConfig.getTemplateUrl() + accessToken, data);
                    System.out.println(res);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    @Override
    public User selectByStuNo(String stuNo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",2);
        queryWrapper.eq("stu_no",stuNo);
        return userMapper.selectOne(queryWrapper);
    }
}
